package com.fortune.controller;

import java.util.List;
import java.util.UUID;

import com.fortune.base.BaseController;
import com.fortune.common.paging.PagingData;
import com.fortune.common.redis.RedisClient;
import com.fortune.constants.redis.CacheKeys;
import com.fortune.util.TokenUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.excution.ClientExcution;
import com.fortune.dto.in.client.ClientAddDto;
import com.fortune.dto.in.client.ClientEditDto;
import com.fortune.dto.in.client.ClientListDto;
import com.fortune.enums.ClientEnum;
import com.fortune.service.ClientService;
import com.fortune.vo.client.Client;
import com.fortune.vo.client.ClientDetail;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "wechat/client", description = "业务员账号相关", tags = {"微信小程序（业务员）：客户管理"})
@RestController
@RequestMapping("wechat/client")
public class ClientController extends BaseController{
	@Autowired
	private RedisClient redisClient;

	@Autowired
	private ClientService clientService;

	@ApiOperation(value = "客户列表", nickname = "mUserList", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "第几页", paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "length", value = "每页容量", paramType = "query", dataType = "int"),
			/*@ApiImplicitParam(name = "search", value = "搜索关键词", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "type", value = "是否已验收（0已验收1未验收）", paramType = "query", dataType = "Integer", allowableValues = "0,1")*/
			@ApiImplicitParam(name = "type",value="获取类型 type：1:有效客户，2:停用客户，3:全部客户",paramType = "query", dataType = "Integer",allowableValues="1,2,3"),
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
			})
	@GetMapping(value = "list",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData> list(@ApiIgnore @Validated ClientListDto clientListDto) {
		//厂商id
		String mid = getMId();
		//经销商id
		String did = getDId();
		/*String phone = redisClient.get(CacheKeys.SALES_MAN_LOGIN_TOKEN, userId);*/
		int type = clientListDto.getType();
		PagingData pagingData = new PagingData();
		if(type == 1){
			List<Client> findEffectiveClient = clientService.findEffectiveClient(mid,did);
			Integer findEffectiveAcount = clientService.findEffectiveAcount(mid, did);
			if(findEffectiveClient == null && findEffectiveAcount == 0){
				return new ClientExcution<PagingData>(pagingData, ClientEnum.FIND_RESULT_AIR);
			}
			pagingData.setData(findEffectiveClient);
			pagingData.setRecordsTotal(findEffectiveAcount);
			return new ClientExcution<PagingData>(pagingData, ClientEnum.FIND_SUCCESS);
		}
		if(type == 2){
			List<Client> findInvalidClient = clientService.findInvalidClient(mid,did);
			Integer findInvalidAcount = clientService.findInvalidAcount(mid, did);
			if(findInvalidClient == null && findInvalidAcount == 0){
				return new ClientExcution<PagingData>(pagingData, ClientEnum.FIND_RESULT_AIR);
			}
			pagingData.setData(findInvalidClient);
			pagingData.setRecordsTotal(findInvalidAcount);
			return new ClientExcution<PagingData>(pagingData, ClientEnum.FIND_SUCCESS);
		}
		if(type == 3){
			List<Client> findAllClient = clientService.findAllClient(mid,did);
			Integer findAllClientAcount = clientService.findAllClientAcount(mid, did);
			if(findAllClient == null && findAllClientAcount == 0){
				return new ClientExcution<PagingData>(pagingData, ClientEnum.FIND_RESULT_AIR);
			}
			pagingData.setData(findAllClient);
			pagingData.setRecordsTotal(findAllClientAcount);
			return new ClientExcution<PagingData>(pagingData, ClientEnum.FIND_SUCCESS);
		}
		return new ClientExcution<PagingData>(pagingData, ClientEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "客户详情", nickname = "mUserDetail", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="clientId", value="客户ID",paramType="query",dataType="string"),
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<ClientDetail> get(@Validated @NotBlank(message="客户id不能为空") String clientId) {
		//厂商id
		String mid = getMId();
		//经销商id
		String did = getDId();
		ClientDetail findClientDetailByClientId = clientService.findClientDetailByClientId(clientId, mid, did);
		if(findClientDetailByClientId == null){
			return new ClientExcution<ClientDetail>(findClientDetailByClientId, ClientEnum.FIND_RESULT_AIR);
		}
		return new ClientExcution<ClientDetail>(findClientDetailByClientId, ClientEnum.FIND_SUCCESS);
	}
	
	@ApiOperation(value = "更改客户信息", nickname = "mUserEdit", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @PutMapping(value = "edit",
    	consumes = { MediaType.APPLICATION_JSON_VALUE },
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(ClientEditDto clientEditDto) {
		//厂商id
		String mid = getMId();
		//经销商id
		String did = getDId();
		int editClient = clientService.editClient(clientEditDto,mid,did);
		if(editClient < 1){
			return new ClientExcution<Void>(null, ClientEnum.EDIT_ERROR);
		}
		return new ClientExcution<Void>(null, ClientEnum.EDIT_SUCCESS);
	}
	
	@ApiOperation(value = "添加客户", nickname = "mUserAdd", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @PostMapping(value = "add",
    	consumes = { MediaType.APPLICATION_JSON_VALUE },
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(@Validated @RequestBody ClientAddDto clientAddDto) {
		/*业务员id*/
		String userId = getUserId();
		//厂商id
		String mid = getMId();
		//经销商id
		String did = getDId();
		String id = UUID.randomUUID().toString().replace("-","");
		int addClient = clientService.addClient(clientAddDto,id,mid,did,userId);
		if(addClient < 1){
			return new ClientExcution<Void>(null, ClientEnum.ADD_ERROR);
		}
		return new ClientExcution<Void>(null, ClientEnum.ADD_SUCCESS);
	}
}
